<?php

namespace App;

use \App\Model;

class Post extends Model
{
    protected $table = 'posts';
    //反向关联用户表
    public function belongsToUser(){
        return $this->belongsTo('App\User','user_id','id');
    }

    //关联评论表
    public function comments(){
        return $this->hasMany('App\Comment','post_id','id')->orderBy('created_at','desc');
    }

    //判断用户是否已经点赞
    public function zan($user_id){
        return $this->hasOne('App\Zan','post_id','id')->where('user_id',$user_id);
    }

    //统计文章总点赞数
    public function zans(){
        return $this->hasMany('App\Zan','post_id','id')->orderBy('created_at','desc');
    }
}
